using System.Collections.Generic;
using System.Linq;
using Swashbuckle.AspNetCore.Swagger;
using Swashbuckle.AspNetCore.SwaggerGen;
using Abp.Authorization;
using System;
using Abp.UI;
using Microsoft.AspNetCore.Http;
using FwProject.Web.ShopCore.Models;

namespace FwProject.Web.ShopHost.Startup
{
    public class SwaggerFileUploadOperationFilter : IOperationFilter
    {
        public void Apply(Operation operation, OperationFilterContext context)

        {

            if (!context.ApiDescription.HttpMethod.Equals("POST", StringComparison.OrdinalIgnoreCase)){

                return;

            }   



            //var fileParameters = context.ApiDescription.ActionDescriptor.Parameters.Where(n => n.ParameterType == typeof(IFormFile)).ToList();
            var fileParameters = context.ApiDescription.ActionDescriptor.Parameters.Where(n => n.Name =="image").ToList();

           
            if (fileParameters.Count < 0)

            {

                return;

            }



            operation.Consumes.Add("multipart/form-data");



            foreach (var fileParameter in fileParameters)

            {

                var parameter = operation.Parameters.Single(n => n.Name == fileParameter.Name);

                operation.Parameters.Remove(parameter);

                operation.Parameters.Add(new NonBodyParameter

                {

                    Name = parameter.Name,

                    In = "formData",

                    Description = parameter.Description,

                    Required = parameter.Required,

                    Type = "file"

                });

            }

        }
       }
    }
